const { filePreview } = require('../../../../api/index');
const { friendshipActivityList } = require('../../../../api/fraternity');
const { formatTime2 } = require('../../../../utils/util');
const { getDict } = require('../../../../utils/dict');

// const { getList } = require('../../api/home');
const {
  isFinished
} = require('./tools');

//  记录弹窗
let model = null;

// pages/home/index.js
Page({
  /*** 页面的初始数据 */
  data: {
    id: '',
    page: {
      pageNumber: 1,
      pageSize: 20,
      friendshipAssociationId: '',
      activityStates: ["00", "01"] //  报名中
    },
    total: 0, //  总条数
    finished: false,
    loading: false,
    status: [],
    dataList: [],
    tabs: [{
        name: '报名中',
        code: [ '00', '01' ]
      },
      {
        name: '进行中',
        code: [ '02', '03' ]
      },
      {
        name: '已结束',
        code: [ '99' ]
      }
    ],
    list: [] //  数据列表
  },
  //  切换 tab 发送请求
  onChangeTabs(event) {
    const { code } = event.detail;
    this.data.page.activityStates = code;
    this.setData({ curTab: code }, () => {
      this.data.page.pageNumber = 1;
      this.data.finished = false;
      this.data.list = [];
      this.setData({ list: [] });
      fecthList.call(this);
    });
  },
  onRefresh() {
    this.data.page.pageNumber = 1;
    this.data.finished = false;
    this.setData({ list: [] }, () => {
      fecthList.call(this);
    })
  },
  loadMore() {  //  加载更多
    fecthList.call(this);
  },
  //  点击事件
  async onBtnClick() {
    model.show();
  },
  /*** 生命周期函数--监听页面加载 */
  async onLoad(options) {
    this.data.id = options.id;
    const res = await getDict('ACTIVITY_STATE');
    this.data.status = res || [];
    fecthList.call(this);
  },

  /*** 生命周期函数--监听页面初次渲染完成 */
  onReady() {

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})


//  获取列表数据
async function fecthList() {
  fetchFriendshipActivityList.call(this);
}

//  获取当前活动热门活动列表
async function fetchFriendshipActivityList() {
  this.data.page.friendshipAssociationId = this.data.id;
  wx.showLoading({ title: '加载中...' });

  const {
    errors,
    result,
    totalCount
  } = await friendshipActivityList(this.data.page);

  if (errors.lenght > 0) {
    return;
  }

  //  判断数据是不是都已经加载完了
  this.setData({
    finished: isFinished(totalCount, this.data.page.pageNumber * this.data.page.pageSize)
  });
  //  加载更多数据
  this.setData({
    list: this.data.list.concat(setResult.call(this, result))
  });
  this.data.page.pageNumber++;

  wx.hideLoading();
}

function setResult(data = []) {
  const _data = JSON.parse(JSON.stringify(data));

  _data.map(item => {
    item.activityBeginTime = formatTime2( item.activityBeginTime );
    item.activityEndTime = formatTime2( item.activityEndTime );
    item.registrationBeginTime = formatTime2( item.registrationBeginTime );
    item.registrationEndTime = formatTime2( item.registrationEndTime );
    item.name = this.data.status.find(_d => _d.code === item.activityState ).name;
    item.status = 'status-' + item.activityState;
    return item;
  })

  return _data;
}